package com.example.whopxx;

import com.example.whopxx.dao.UserDao;
import com.example.whopxx.domain.RegFormBean;
import com.example.whopxx.domain.UserBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "RegServlet", value = "/RegServlet") // 注册表单提交
public class RegServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        String id = request.getParameter("id");
        String flag = "add";
        if (!id.isEmpty()) {
            flag = "edit";
        }

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String repassword = request.getParameter("repassword");
        String truename = request.getParameter("truename");

        String gender = request.getParameter("gender");
        String phone = request.getParameter("phone");

        String email = request.getParameter("email");
        String cardNo = request.getParameter("cardNo");
        String card = request.getParameter("card");

        RegFormBean regFormBean = new RegFormBean();
        regFormBean.setUsername(username);
        regFormBean.setPassword(password);
        regFormBean.setRepassword(repassword);
        regFormBean.setTruename(truename);
        regFormBean.setGender(gender);
        regFormBean.setPhone(phone);
        regFormBean.setEmail(email);
        regFormBean.setCard(card);
        regFormBean.setCardNo(cardNo);

        if (!regFormBean.validate()) {
            request.setAttribute("formBean", regFormBean);
            request.getRequestDispatcher("reg.jsp").forward(request, response);

            return;
        }

        UserBean userBean = new UserBean();

        userBean.setId(id.isEmpty() ? 0 : Integer.parseInt(id));
        userBean.setUsername(username);
        userBean.setPassword(password);
        userBean.setTruename(truename);
        userBean.setGender(gender);
        userBean.setPhone(phone);
        userBean.setEmail(email);
        userBean.setCardNo(cardNo);
        userBean.setCard(card);

        UserDao userDao = new UserDao();
        Boolean f = false;

        if (flag.equals("edit")) //修改
        {
            f = userDao.updateUser(userBean);
            response.getWriter().print("恭喜你操作成功，3秒钟自动跳转");
            request.getSession().setAttribute("userBean", userBean);
            request.getRequestDispatcher("IndexServlet").forward(request, response);
        } else {
            f = userDao.insert(userBean);
            response.getWriter().print("恭喜你操作成功，3秒钟自动跳转");
            request.getSession().setAttribute("userBean", userBean);
            response.setHeader("refresh", "3;url=regSuccess.jsp");

        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        doGet(request, response);
    }
}